{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "92b92e35",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679281854,33776adb-a311-462a-a2ab-78fc691e79a8', 'time_used': 676, 'confidence': 81.31, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}, 'faces1': [{'face_token': 'f50ffa6f7ecafd053947eb57062e8850', 'face_rectangle': {'top': 582, 'left': 340, 'width': 673, 'height': 673}}], 'faces2': [{'face_token': 'e2e243a014e55a64da13085a3f3d23ac', 'face_rectangle': {'top': 172, 'left': 230, 'width': 201, 'height': 201}}], 'image_id1': 'JCtR+/0G5deze9yfrHaHHQ==', 'image_id2': 'n+P8GORw0D9N1cbYJFW/ow=='}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "f_01 = open('cxk1.jpg', 'rb')\n",
    "f_02 = open('cxk2.jpg', 'rb')\n",
    "img_01 = base64.b64encode(f_01.read())\n",
    "img_02 = base64.b64encode(f_02.read())\n",
    "\n",
    "params = {\n",
    "    \"api_key\":\"BkCUjhbzbcer0M90BPVlnlh_PDr3auLo\",\n",
    "    \"api_secret\":\"qnh3_Qh8-ik9EW9W3b56yBEyAbBul_7Z\",\n",
    "    \"image_base64_1\":img_01,\n",
    "    \"image_base64_2\":img_02,\n",
    "         }\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93b43c9a",
   "metadata": {},
   "source": [
    "## 2.1.1人脸库管理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "91ab2f82",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'faceset_token': '756d4ed49c75e0b5cbf5a646389ab944', 'time_used': 170, 'face_count': 0, 'face_added': 0, 'request_id': '1679283452,80c05798-a2ca-43b4-9f77-8b651778114e', 'outer_id': '', 'failure_detail': []}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"BkCUjhbzbcer0M90BPVlnlh_PDr3auLo\",\n",
    "    \"api_secret\":\"qnh3_Qh8-ik9EW9W3b56yBEyAbBul_7Z\",\n",
    "    \"faceset_token\":\"e2e243a014e55a64da13085a3f3d23ac\",\n",
    " }\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40c34a47",
   "metadata": {},
   "source": [
    "## 2.1.2检查人脸数据集的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "4c7ba0a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'time_used': 92, 'facesets': [{'faceset_token': 'e3e0d42c0d410c58fc8a875c14eee1f2', 'outer_id': '', 'display_name': '广州南方学院1栋宿舍楼', 'tags': ''}, {'faceset_token': '890d756891717b8a51f3dcb21ea47247', 'outer_id': '', 'display_name': '', 'tags': ''}, {'faceset_token': '756d4ed49c75e0b5cbf5a646389ab944', 'outer_id': '', 'display_name': '', 'tags': ''}], 'request_id': '1679284487,c693d816-b16c-4498-8bf2-84f8dd2a7b63'}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"BkCUjhbzbcer0M90BPVlnlh_PDr3auLo\",\n",
    "    \"api_secret\":\"qnh3_Qh8-ik9EW9W3b56yBEyAbBul_7Z\",\n",
    "    \"faceset_token\":\"广州南方学院1栋宿舍楼\",\n",
    " }\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef279435",
   "metadata": {},
   "source": [
    "## 2.1.3 添加人脸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "72fa5f80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679284386,fb1e8972-c3b2-46cb-b78c-f5dc1129e605', 'time_used': 146, 'faces': [{'face_token': '0bb41f13b947fe2b194a9026dc8166f0', 'face_rectangle': {'top': 43, 'left': 62, 'width': 38, 'height': 38}, 'attributes': {'gender': {'value': 'Male'}, 'age': {'value': 30}, 'smile': {'value': 0.685, 'threshold': 50.0}, 'headpose': {'pitch_angle': -3.901899, 'roll_angle': -1.0087477, 'yaw_angle': -2.2193658}, 'blur': {'blurness': {'value': 18.585, 'threshold': 50.0}, 'motionblur': {'value': 18.585, 'threshold': 50.0}, 'gaussianblur': {'value': 18.585, 'threshold': 50.0}}, 'emotion': {'anger': 0.783, 'disgust': 0.008, 'fear': 0.008, 'happiness': 0.101, 'neutral': 98.852, 'sadness': 0.241, 'surprise': 0.008}, 'facequality': {'value': 84.354, 'threshold': 70.1}, 'beauty': {'male_score': 62.792, 'female_score': 63.153}}}], 'image_id': '73mNJqY5euFqvH/K8yKGPg==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸检测\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('一栋/viper.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "payload = {\n",
    "    'api_key':'jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov',\n",
    "    'api_secret':'Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3',\n",
    "    'image_base64':img,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,emotion,blur,beauty'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data = payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "775e0ccf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'time_used': 55, 'error_message': 'INVALID_FACESET_TOKEN', 'request_id': '1679284471,fbba1430-c168-44a2-9e1d-7e11d5cce7e2'}\n",
      "400\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "添加人脸\n",
    "'''\n",
    "\n",
    "request_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"BkCUjhbzbcer0M90BPVlnlh_PDr3auLo\",\n",
    "    \"api_secret\":\"qnh3_Qh8-ik9EW9W3b56yBEyAbBul_7Z\",\n",
    "    \"faceset_token\":\"e2e243a014e55a64da13085a3f3d23ac\",\n",
    "    \"face_tokens\":[ '0bb41f13b947fe2b194a9026dc8166f0']\n",
    " }\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8082dc90",
   "metadata": {},
   "source": [
    "## 2.1.4 人脸搜索（课后作业）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29197735",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
